package com.goods.controller.business;


import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "业务模块-物资分类相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    //1.1 /business/productCategory/categoryTree?pageNum=1&pageSize=5
    @ApiOperation(value = "物资分类列表")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(
            Integer pageNum, Integer pageSize) {

        PageVO<ProductCategoryTreeNodeVO> pageVO = productCategoryService.categoryTree(pageNum, pageSize);
        return ResponseBean.success(pageVO);
    }

    //1.2.1 /business/productCategory/getParentCategoryTree
    @ApiOperation(value = "物资分类父级列表")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> list = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(list);
    }

    //1.2.2 /business/productCategory/add
    @ApiOperation(value = "添加分类")
    @PostMapping("/add")
    public ResponseBean addParentCategoryTree(@RequestBody ProductCategory productCategory) {
        productCategoryService.addParentCategoryTree(productCategory);
        return ResponseBean.success();
    }

    //1.3.1 /business/productCategory/edit/id
    @ApiOperation(value = "修改回显编辑分类")
    @GetMapping("/edit/{id}")
    public ResponseBean editParentCategoryTree(@PathVariable("id") Long id) {
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = productCategoryService.editParentCategoryTree(id);
        return ResponseBean.success(productCategoryTreeNodeVO);

    }

    //1.3.2 /business/productCategory/update/id
    @ApiOperation(value = "修改分类")
    @PutMapping("/update/{id}")
    public ResponseBean updateParentCategoryTree(@PathVariable("id") Long id, @RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        productCategoryService.updateParentCategoryTree(id, productCategoryTreeNodeVO);
        return ResponseBean.success();

    }

    //1.4 /business/productCategory/delete/id
    @ApiOperation(value = "删除分类")
    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteParentCategoryTree(@PathVariable("id") Long id) {
        boolean b = productCategoryService.deleteParentCategoryTree(id);
        if (b) {
            return ResponseBean.success();
        }
        Map<String, String> map = new HashMap<>();
        map.put("errorMsg", "父分类有子分类,分类删除失败");
        return ResponseBean.error(map);
    }
}
